package api_sys

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wrblog-api-go/app/common/token"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/app/service/service_sys"
	"wrblog-api-go/pkg/request"
	"wrblog-api-go/pkg/result"
	"wrblog-api-go/pkg/status"
)

// @Tags  System - 参数管理
// @Summary  刷新缓存
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/refCache [get]
func ApiCacheConfigList(c *gin.Context) {
	result.Res(true, service_sys.CacheConfigList()).Json(c)
}

// @Tags  System - 参数管理
// @Summary  获取参数列表
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysConfig query model_sys.SelectSysConfig true "sysConfig"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/page [get]
func ApiSysConfigPageList(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysConfig := raw.(model_sys.SelectSysConfig)
	result.PageRes(service_sys.GetSysConfigPageList(sysConfig)).Json(c)
}

// @Tags  System - 参数管理
// @Summary  获取参数详情
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param configId path int true "configId"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/{configId} [get]
func ApiSysConfigById(c *gin.Context) {
	configId, err := strconv.Atoi(c.Param("configId"))
	if err != nil {
		result.Wrap(status.BadRequest, "参数读取失败").Json(c)
	} else {
		sysConfig, err := service_sys.GetSysConfigById(configId)
		if err != nil {
			result.Fail(err).Json(c)
		} else {
			result.Ok(sysConfig).Json(c)
		}
	}
}

// @Tags  System - 参数管理
// @Summary  添加参数
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysConfig body model_sys.SysConfig true "sysConfig"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/add [post]
func ApiAddSysConfig(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysConfig := raw.(model_sys.SysConfigPo)
	sysConfig.CreateBy = token.GetUk(c)
	result.Res(service_sys.AddSysConfig(sysConfig)).Json(c)
}

// @Tags  System - 参数管理
// @Summary  修改参数
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param sysConfig body model_sys.SysConfig true "sysConfig"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/edit [put]
func ApiEditSysConfig(c *gin.Context) {
	raw, _ := c.Get(request.BindObj)
	sysConfig := raw.(model_sys.SysConfigPo)
	sysConfig.CreateBy = token.GetUk(c)
	result.Res(service_sys.EditSysConfig(sysConfig)).Json(c)
}

// @Tags  System - 参数管理
// @Summary  删除参数[逻辑删除]
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization"
// @Param configIds query string true "configIds"
// @Success 200 {object} result.Result "OK"
// @Router /sysConfig/removes [delete]
func ApiRemoveSysConfig(c *gin.Context) {
	configIds := c.Query("configIds")
	i, err := service_sys.RemoveSysConfig(configIds, token.GetUk(c))
	result.Res(i, err).Json(c)
}
